package o2o.web.actions.sys;

import o2o.config.dao.UserDaoSql;
import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.ObjectUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.sys.SysVariableDistributeMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.sys.SysVariableDistribute;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.sys.SysVariableDistributeVO;
import o2o.web.actions.user.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

//系统变量

@Controller
public class SysVariableDistributeAction {

    //获取分成设置
    @RequestMapping(method = RequestMethod.POST, value = "/sys/variable/distribute/list")
    @ResponseBody
    public String sys_variable_list(String token, Integer orderType) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (null == orderType) {
            return AjaxResult.errorResult("请选择订单类型");
        }
        List<SysVariableDistribute> list = SysVariableDistributeMemory.getInstance().listOfOrderType(orderType);


        return AjaxResult.successResult(list.size(), list);
    }

    //获取分成设置 --老克
    @RequestMapping(method = RequestMethod.POST, value = "/sys/variable/distribute/list/cat1")
    @ResponseBody
    public String sys_variable_ca1_list(String token, Integer orderType) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (null == orderType) {
            return AjaxResult.errorResult("请选择订单类型");
        }

//        //等级list
//        List<Integer> levelList = SysVariableDistributeMemory.getInstance().getTypeList(1);
//        //制单人list
//        List<Integer> submitUserList = SysVariableDistributeMemory.getInstance().getTypeList(2);
//        //执行人list
//        List<Integer> opUserList = SysVariableDistributeMemory.getInstance().getTypeList(3);
//        //费用类型list
//        List<Integer> moneyTypeList = SysVariableDistributeMemory.getInstance().getTypeList(4);

        //等级list
        List<Integer> type1List = SysVariableDistributeMemory.getInstance().getTypeList(1);
        if (ValidateUtil.isAbsList(type1List)) {//等级列表
            List<SysVariableDistributeVO> list = new ArrayList<>();
            for (int i = 0; i < type1List.size(); i++) {
                Integer level = type1List.get(i);
                if (null == level) {
                    continue;
                }

                //等级
                SysVariableDistributeVO levelVo = new SysVariableDistributeVO();
                levelVo.setType1(level);//等级 type1

                List<SysVariableDistributeVO> levelVoList = new ArrayList<>();
                //等级下的所有数据
                List<SysVariableDistribute> levelVarList = SysVariableDistributeMemory.getInstance().listOfType(orderType, level, null, null, null);
                if (ValidateUtil.isAbsList(levelVarList)) {
                    List<Integer> type4List = getTypeList(levelVarList, 4);//该等级下制单人类型列表
                    if (ValidateUtil.isAbsList(type4List)) {//制单人列表 type4List
                        for (int j = 0; j < type4List.size(); j++) {
                            Integer submitUserType = type4List.get(j);
                            if (null == submitUserType) {
                                continue;
                            }

                            //制单人类型
                            SysVariableDistributeVO submitUserTypeVo = new SysVariableDistributeVO();
                            submitUserTypeVo.setType4(submitUserType);//制单人类型 type4

                            List<SysVariableDistributeVO> moneyTypeVOList = new ArrayList<>();
                            //制单人类型的下的所有数据
                            List<SysVariableDistribute> submitUserTypeList = listOfType(levelVarList, level, null, null, submitUserType);
                            if (ValidateUtil.isAbsList(submitUserTypeList)) {
                                List<Integer> type3List = getTypeList(submitUserTypeList, 3);//该制单人下的 费用类型列表
                                if (ValidateUtil.isAbsList(type3List)) {//费用类型列表
                                    for (int k = 0; k < type3List.size(); k++) {
                                        Integer moneyType = type3List.get(k);
                                        if (null == moneyType) {
                                            continue;
                                        }
                                        //费用类型
                                        SysVariableDistributeVO moneyVo = new SysVariableDistributeVO();
                                        if (0 == -1 - moneyType) {//-1为 平台手续费
                                            List<SysVariableDistribute> varList = listOfType(submitUserTypeList, level, 0, moneyType, submitUserType);
                                            if (ValidateUtil.isAbsList(varList)) {
//                                                SysVariableDistributeVO valVo = new SysVariableDistributeVO();
                                                try {
                                                    ObjectUtils.copyPropertiesExclude(varList.get(0), moneyVo);
                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        } else {
                                            moneyVo.setType3(moneyType);//费用类型 type3

                                            List<SysVariableDistributeVO> opUserTypeVOList = new ArrayList<>();
                                            //等级下的所有数据
                                            List<SysVariableDistribute> opUserTypeList = listOfType(submitUserTypeList, level, null, moneyType, submitUserType);
                                            if (ValidateUtil.isAbsList(opUserTypeList)) {
                                                List<Integer> opTypeList = getTypeList(opUserTypeList, 2);//该费用类型下的 执行人列表
                                                if (ValidateUtil.isAbsList(opTypeList)) {
                                                    for (int l = 0; l < opTypeList.size(); l++) {
                                                        Integer opUserType = opTypeList.get(l);
                                                        if (null == opUserType) {
                                                            continue;
                                                        }
                                                        //执行人类型
                                                        SysVariableDistributeVO valVo = new SysVariableDistributeVO();
                                                        List<SysVariableDistribute> distributeList = listOfType(opUserTypeList, level, opUserType, moneyType, submitUserType);
                                                        if (ValidateUtil.isAbsList(distributeList)) {
                                                            try {
                                                                ObjectUtils.copyPropertiesExclude(distributeList.get(0), valVo);
                                                            } catch (Exception e) {
                                                                e.printStackTrace();
                                                            }
                                                        }

                                                        opUserTypeVOList.add(valVo);//费用类型列表对应的数据
                                                    }
                                                }
                                            }
                                            moneyVo.setChildList(opUserTypeVOList);//type3 childList
                                        }

                                        moneyTypeVOList.add(moneyVo);//费用类型列表对应的数据
                                    }
                                }
                            }


//                            List<SysVariableDistributeVO> opUserVOList = new ArrayList<>();
//                            if (ValidateUtil.isAbsList(opUserList)) {
//                                for (int k = 0; k < opUserList.size(); k++) {
//                                    Integer opUserType = opUserList.get(k);
//                                    if (null == opUserType) {
//                                        continue;
//                                    }
//
//                                    //执行人类型
//                                    SysVariableDistributeVO opUserTypeVo = new SysVariableDistributeVO();
//                                    opUserTypeVo.setType2(opUserType);//执行人类型 type2
//
//                                    List<SysVariableDistributeVO> moneyTypeVOList = new ArrayList<>();
//                                    //等级下的所有数据
//                                    List<SysVariableDistribute> opUserTypeList =listOfType(submitUserTypeList, level, opUserType, null, submitUserType);
//                                    if (!ValidateUtil.isAbsList(opUserTypeList)) {
//                                        List<Integer> moneyTypeList = getTypeList(opUserTypeList, 3);//该制单人下的 执行人列表
//                                        if (ValidateUtil.isAbsList(moneyTypeList)) {
//                                            List<SysVariableDistributeVO> moneyList = new ArrayList<>();
//                                            for (int l = 0; l < moneyTypeList.size(); l++) {
//                                                Integer moneyType = moneyTypeList.get(l);
//                                                if (null == moneyType) {
//                                                    continue;
//                                                }
//                                                //费用类型
//                                                SysVariableDistributeVO moneyVo = new SysVariableDistributeVO();
////                                                moneyVo.setType3(moneyType);//费用类型 type3
//                                                List<SysVariableDistribute> distributeList = listOfType(opUserTypeList, level, opUserType, moneyType, submitUserType);
//                                                if (ValidateUtil.isAbsList(distributeList)) {
//                                                    try {
//                                                        ObjectUtils.copyPropertiesExclude(distributeList.get(0), moneyVo);
//                                                    } catch (Exception e) {
//                                                        e.printStackTrace();
//                                                    }
//                                                }
//
//                                                moneyList.add(moneyVo);//费用类型列表对应的数据
//                                            }
//                                        }
//                                    }
//
//
//                                    opUserTypeVo.setChildList(moneyTypeVOList);
//                                    opUserVOList.add(opUserTypeVo);//执行人下的数据
//                                }
//                            }

                            submitUserTypeVo.setChildList(moneyTypeVOList);//type4 childList
                            levelVoList.add(submitUserTypeVo);//制单人类型下的数据
                        }
                    }
                }


                levelVo.setChildList(levelVoList);//type1 childList
                list.add(levelVo);//等级下的所有数据
            }

            return AjaxResult.successResult(list.size(), list);
        }


        return AjaxResult.successResult(0, null);
    }

    //获取分成设置 --老克
//    @RequestMapping(method = RequestMethod.POST, value = "/sys/variable/distribute/list/cat2")
//    @ResponseBody
//    public String sys_variable_ca2_list(String token, Integer orderType) {
//        //验证用户有效性
//        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
//        if (user == null) {
//            return AjaxResult.errorLogoutResult();
//        }
//
//        if (null == orderType) {
//            return AjaxResult.errorResult("请选择订单类型");
//        }
//
//
//        List<Integer> type4List = SysVariableDistributeMemory.getInstance().getTypeList(4);
//        ;//该等级下制单人类型列表
//        if (ValidateUtil.isAbsList(type4List)) {//制单人列表 type4List
//            List<SysVariableDistributeVO> list = new ArrayList<>();
//            for (int j = 0; j < type4List.size(); j++) {
//                Integer submitUserType = type4List.get(j);
//                if (null == submitUserType) {
//                    continue;
//                }
//
//                //制单人类型
//                SysVariableDistributeVO submitUserTypeVo = new SysVariableDistributeVO();
//                submitUserTypeVo.setType4(submitUserType);//制单人类型 type4
//
//                List<SysVariableDistributeVO> moneyTypeVOList = new ArrayList<>();
//                //制单人类型的下的所有数据
//                List<SysVariableDistribute> submitUserTypeList = SysVariableDistributeMemory.getInstance().listOfType(orderType, null, null, null, submitUserType);
//                if (ValidateUtil.isAbsList(submitUserTypeList)) {
//                    List<Integer> type3List = getTypeList(submitUserTypeList, 3);//该制单人下的 费用类型列表
//                    if (ValidateUtil.isAbsList(type3List)) {//费用类型列表
//                        for (int k = 0; k < type3List.size(); k++) {
//                            Integer moneyType = type3List.get(k);
//                            if (null == moneyType) {
//                                continue;
//                            }
//                            //费用类型
//                            SysVariableDistributeVO moneyVo = new SysVariableDistributeVO();
//                            if (0 == -1 - moneyType) {//-1为 平台手续费
//                                List<SysVariableDistribute> varList = listOfType(submitUserTypeList, null, 0, moneyType, submitUserType);
//                                if (ValidateUtil.isAbsList(varList)) {
////                                                SysVariableDistributeVO valVo = new SysVariableDistributeVO();
//                                    try {
//                                        ObjectUtils.copyPropertiesExclude(varList.get(0), moneyVo);
//                                    } catch (Exception e) {
//                                        e.printStackTrace();
//                                    }
//                                }
//                            } else {
//                                moneyVo.setType3(moneyType);//费用类型 type3
//
//                                List<SysVariableDistributeVO> opUserTypeVOList = new ArrayList<>();
//                                //等级下的所有数据
//                                List<SysVariableDistribute> opUserTypeList = listOfType(submitUserTypeList, null, null, moneyType, submitUserType);
//                                if (ValidateUtil.isAbsList(opUserTypeList)) {
//                                    List<Integer> opTypeList = getTypeList(opUserTypeList, 2);//该费用类型下的 执行人列表
//                                    if (ValidateUtil.isAbsList(opTypeList)) {
//                                        for (int l = 0; l < opTypeList.size(); l++) {
//                                            Integer opUserType = opTypeList.get(l);
//                                            if (null == opUserType) {
//                                                continue;
//                                            }
//                                            //执行人类型
//                                            SysVariableDistributeVO valVo = new SysVariableDistributeVO();
//                                            moneyVo.setType2(opUserType);//执行人类型 type2
//                                            List<SysVariableDistributeVO> opUserTypeVOList = new ArrayList<>();
//                                            //等级下的所有数据
//                                            List<SysVariableDistribute> opUserTypeList = listOfType(submitUserTypeList, null, null, moneyType, submitUserType);
//
//                                            List<SysVariableDistribute> distributeList = listOfType(opUserTypeList, null, opUserType, moneyType, submitUserType);
//                                            if (ValidateUtil.isAbsList(distributeList)) {
//                                                try {
//                                                    ObjectUtils.copyPropertiesExclude(distributeList.get(0), valVo);
//                                                } catch (Exception e) {
//                                                    e.printStackTrace();
//                                                }
//                                            }
//
//                                            opUserTypeVOList.add(valVo);//费用类型列表对应的数据
//                                        }
//                                    }
//                                }
//                                moneyVo.setChildList(opUserTypeVOList);//type3 childList
//                            }
//
//                            moneyTypeVOList.add(moneyVo);//费用类型列表对应的数据
//                        }
//                    }
//                }
//
//
//                submitUserTypeVo.setChildList(moneyTypeVOList);//type4 childList
//                list.add(submitUserTypeVo);//制单人类型下的数据
//            }
//            return AjaxResult.successResult(list.size(), list);
//        }
//
//
//        return AjaxResult.successResult(0, null);
//    }

//    //获取分成设置 --老克
//    @RequestMapping(method = RequestMethod.POST, value = "/sys/variable/distribute/list/cat1")
//    @ResponseBody
//    public String sys_variable_ca1_list(String token, Integer orderType) {
//        //验证用户有效性
//        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
//        if (user == null) {
//            return AjaxResult.errorLogoutResult();
//        }
//
//        if (null == orderType) {
//            return AjaxResult.errorResult("请选择订单类型");
//        }
//
//        //等级list
//        List<Integer> levelList = SysVariableDistributeMemory.getInstance().getTypeList(1);
////        //制单人list
////        List<Integer> submitUserList = SysVariableDistributeMemory.getInstance().getTypeList(2);
////        //执行人list
////        List<Integer> opUserList = SysVariableDistributeMemory.getInstance().getTypeList(3);
////        //费用类型list
////        List<Integer> moneyTypeList = SysVariableDistributeMemory.getInstance().getTypeList(4);
//
//        if (ValidateUtil.isAbsList(levelList)) {
//            List<SysVariableDistributeVO> list = new ArrayList<>();
//            for (int i = 0; i < levelList.size(); i++) {
//                Integer level = levelList.get(i);
//                if (null == level) {
//                    continue;
//                }
//
//                //等级
//                SysVariableDistributeVO levelVo = new SysVariableDistributeVO();
//                levelVo.setType1(level);//等级 type1
//
//                List<SysVariableDistributeVO> levelVoList = new ArrayList<>();
//                //等级下的所有数据
//                List<SysVariableDistribute> levelVarList = SysVariableDistributeMemory.getInstance().listOfType(orderType, level, null, null, null);
//                if (ValidateUtil.isAbsList(levelVarList)) {
//                    List<Integer> submitUserList = getTypeList(levelVarList, 4);//该等级下制单人类型列表
//                    if (ValidateUtil.isAbsList(submitUserList)) {
//                        for (int j = 0; j < submitUserList.size(); j++) {
//                            Integer submitUserType = submitUserList.get(j);
//                            if (null == submitUserType) {
//                                continue;
//                            }
//
//                            //制单人类型
//                            SysVariableDistributeVO submitUserTypeVo = new SysVariableDistributeVO();
//                            submitUserTypeVo.setType4(submitUserType);//制单人类型 type4
//
//
//                            //制单人类型的所有数据
//                            List<SysVariableDistribute> submitUserTypeList = listOfType(levelVarList, level, null, null, submitUserType);
//                            List<Integer> opUserList = getTypeList(submitUserTypeList, 2);//该制单人下的 执行人列表
//
//                            List<SysVariableDistributeVO> opUserVOList = new ArrayList<>();
//                            if (ValidateUtil.isAbsList(opUserList)) {
//                                for (int k = 0; k < opUserList.size(); k++) {
//                                    Integer opUserType = opUserList.get(k);
//                                    if (null == opUserType) {
//                                        continue;
//                                    }
//
//                                    //执行人类型
//                                    SysVariableDistributeVO opUserTypeVo = new SysVariableDistributeVO();
//                                    opUserTypeVo.setType2(opUserType);//执行人类型 type2
//
//                                    List<SysVariableDistributeVO> moneyTypeVOList = new ArrayList<>();
//                                    //等级下的所有数据
//                                    List<SysVariableDistribute> opUserTypeList =listOfType(submitUserTypeList, level, opUserType, null, submitUserType);
//                                    if (!ValidateUtil.isAbsList(opUserTypeList)) {
//                                        List<Integer> moneyTypeList = getTypeList(opUserTypeList, 3);//该制单人下的 执行人列表
//                                        if (ValidateUtil.isAbsList(moneyTypeList)) {
//                                            List<SysVariableDistributeVO> moneyList = new ArrayList<>();
//                                            for (int l = 0; l < moneyTypeList.size(); l++) {
//                                                Integer moneyType = moneyTypeList.get(l);
//                                                if (null == moneyType) {
//                                                    continue;
//                                                }
//                                                //费用类型
//                                                SysVariableDistributeVO moneyVo = new SysVariableDistributeVO();
////                                                moneyVo.setType3(moneyType);//费用类型 type3
//                                                List<SysVariableDistribute> distributeList = listOfType(opUserTypeList, level, opUserType, moneyType, submitUserType);
//                                                if (ValidateUtil.isAbsList(distributeList)) {
//                                                    try {
//                                                        ObjectUtils.copyPropertiesExclude(distributeList.get(0), moneyVo);
//                                                    } catch (Exception e) {
//                                                        e.printStackTrace();
//                                                    }
//                                                }
//
//                                                moneyList.add(moneyVo);//费用类型列表对应的数据
//                                            }
//                                        }
//                                    }
//
//
//                                    opUserTypeVo.setChildList(moneyTypeVOList);
//                                    opUserVOList.add(opUserTypeVo);//执行人下的数据
//                                }
//                            }
//
//
//                            submitUserTypeVo.setChildList(opUserVOList);
//                            levelVoList.add(submitUserTypeVo);//制单人类型下的数据
//                        }
//                    }
//                }
//
//
//                levelVo.setChildList(levelVoList);
//                list.add(levelVo);//等级下的所有数据
//            }
//
//            return AjaxResult.successResult(list.size(), list);
//        }
//
//
//        return AjaxResult.successResult(0, null);
//    }

    //修改分成设置
    @RequestMapping(method = RequestMethod.POST, value = "/sys/variable/distribute/modify")
    @ResponseBody
    public String sys_variable_modify(String token, SysVariableDistribute info) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (!User.hasSuperAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }

        if (null == info) {
            return AjaxResult.errorResult("请选择您要修改的");
        }

        if (null == info.getId()) {
            return AjaxResult.errorResult("请选择您要修改的!");
        }

        if (null == info.getVal()) {
            return AjaxResult.errorResult("请填写需要设置的参数值");
        }


        SysVariableDistribute variable = SysVariableDistributeMemory.getInstance().getById(info.getId());
        if (null == variable) {
            return AjaxResult.errorResult("您要修改的不存在，请重试");
        }

        if (null == info.getVal()) {
            info.setVal(variable.getVal());
        }
        if (null == info.getName()) {
            info.setName(variable.getName());
        }
        if (null == info.getNote()) {
            info.setNote(variable.getNote());
        }
        if (null == info.getDescs()) {
            info.setDescs(variable.getDescs());
        }
        if (null == info.getExtra1()) {
            info.setExtra1(variable.getExtra1());
        }
        if (null == info.getExtra2()) {
            info.setExtra2(variable.getExtra2());
        }
        if (null == info.getExtra3()) {
            info.setExtra3(variable.getExtra3());
        }

        StringBuffer sb = new StringBuffer();
        sb.append("update ").append(PojoTables.SYS_VARIABLE_DISTRIBUTE).append(" set name='")
                .append(info.getName()).append("',val=").append(info.getVal()).append(",note='").append(info.getNote())
                .append("',descs='").append(info.getDescs()).append("',extra1='").append(info.getExtra1())
                .append("',extra2='").append(info.getExtra2()).append("',extra3='").append(info.getExtra3())
                .append("',updateUser='").append(user.getUsername()).append("'  where delFlag = 0 and id = ").append(info.getId());
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("修改失败，请稍后再试");
        }

        SysVariableDistributeMemory.getInstance().modify(info.getId());

        return AjaxResult.successResult();
    }


    //---------------------筛选list
    //获取类型列表
    private List<Integer> getTypeList(List<SysVariableDistribute> list, Integer typeFlag) {
        if (!ValidateUtil.isAbsList(list)) {
            return null;
        }
        if (null == typeFlag) {
            return null;
        }
        ConcurrentHashMap<Integer, Long> typeMap = new ConcurrentHashMap<>();
        for (int i = 0; i < list.size(); i++) {
            SysVariableDistribute info = list.get(i);
            if (null == info) {
                continue;
            }
            switch (typeFlag) {
                case 1: {
                    if (null != info.getType1()) {
                        typeMap.put(info.getType1(), info.getId());
                    }
                }
                break;
                case 2: {
                    if (null != info.getType2()) {
                        typeMap.put(info.getType2(), info.getId());
                    }
                }
                break;
                case 3: {
                    if (null != info.getType3()) {
                        typeMap.put(info.getType3(), info.getId());
                    }
                }
                break;
                case 4: {
                    if (null != info.getType4()) {
                        typeMap.put(info.getType4(), info.getId());
                    }
                }
                break;
            }
        }
        if (null != typeMap && typeMap.size() > 0) {
            List<Integer> typeList = new ArrayList<>();
            for (Integer key : typeMap.keySet()) {
                if (null == key) {
                    continue;
                }
                typeList.add(key);
            }
            return typeList;
        }


        return null;
    }

    private List<SysVariableDistribute> listOfType(List<SysVariableDistribute> list, Integer type1, Integer type2, Integer type3, Integer type4) {
        if (!ValidateUtil.isAbsList(list)) {
            return null;
        }

        List<SysVariableDistribute> voList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            SysVariableDistribute info = list.get(i);
            if (null == info) {
                continue;
            }

            if (null != type1 && (null == info.getType1() || 0 != type1 - info.getType1())) {
                continue;
            }
            if (null != type2 && (null == info.getType2() || 0 != type2 - info.getType2())) {
                continue;
            }
            if (null != type3 && (null == info.getType3() || 0 != type3 - info.getType3())) {
                continue;
            }
            if (null != type4 && (null == info.getType4() || 0 != type4 - info.getType4())) {
                continue;
            }

            voList.add(info);
        }

        return voList;
    }
}
